package com.eies.business.applicate.dbsjcx;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import com.eies.common.LogWritter;

import ctais.services.data.DataWindow;
import ctais.util.StringEx;

public class DbsjcxManager {
	public String queryXxdxx(String xml) throws Exception {
		String classPath = "";
		Map headMap = new HashMap();
		headMap.put("design","http://www.chinatax.gov.cn/tirip/dataspec");
		SAXReader saxReader = new SAXReader();
	    saxReader.getDocumentFactory().setXPathNamespaceURIs(headMap);
	    Document document = saxReader.read(new StringReader(xml));
        XPath x_serviceId = document.createXPath("design:tiripPackage/design:identity/design:serviceId");
        XPath x_channelId = document.createXPath("design:tiripPackage/design:identity/design:channelId");
        XPath x_password = document.createXPath("design:tiripPackage/design:identity/design:password");
        XPath x_content = document.createXPath("design:tiripPackage/design:businessContent/design:subPackage/design:content");
        Element serviceId = (Element) x_serviceId.selectSingleNode(document);
		Element channelId = (Element) x_channelId.selectSingleNode(document);
		Element password = (Element) x_password.selectSingleNode(document);
		Element content = (Element) x_content.selectSingleNode(document);
		Map contentMap = new HashMap();
	    contentMap.put("content","http://www.chinatax.gov.cn/dataspec/");
        SAXReader Reader = new SAXReader();
        Reader.getDocumentFactory().setXPathNamespaceURIs(contentMap);
		Document contentDocument = Reader.read(new StringReader(content.getText()));
		 XPath x_zzjgmc = contentDocument.createXPath("content:taxML/content:ZZJGXX/content:JGMC");
		 XPath x_zzjgdm = contentDocument.createXPath("content:taxML/content:ZZJGXX/content:ZZJGDM");
		Element zzjgdm = (Element) x_zzjgdm.selectSingleNode(contentDocument);
		Element zzjgmc = (Element) x_zzjgmc.selectSingleNode(contentDocument);
		CxBean bean = new CxBean();
		bean.setZzjgdm(zzjgdm.getText());
		bean.setZzjgmc(zzjgmc.getText());
		String sql = "select * from CS_DBSJCX where XXDDM='"
				+ serviceId.getText() + "' and channelId='"
				+ channelId.getText() + "' and PASSWORD='" + password.getText()
				+ "'";
		LogWritter.debug("DbsjcxManager:" +sql,DbsjcxManager.class);
		DataWindow dw = DataWindow.dynamicCreate(sql);
		long rows = dw.retrieve();
		if (rows > 0) {
			classPath = StringEx.sNull(dw.getItemAny(0, "CLASSPATH"));
		}
		Class classBean = Class.forName(classPath);
		DbsjcxInf component = (DbsjcxInf) classBean.newInstance();
		String result = component.execute(bean);
		content.clearContent();
		content.addCDATA(result);
		return document.asXML();
	}
}
